package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.game.common.ItemResponse;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class MailListResponse extends Response {

	private List<Mail> mails;
	
	public static class Mail {
		long roleId; // dest player id
		int id; // mail id
		String title;
		String content;
		long createTime;
		byte checked; // 是否内容已读
		byte received; // 是否领取附件
		Object extra; // 附件
		public long getRoleId() {
			return roleId;
		}
		public void setRoleId(long roleId) {
			this.roleId = roleId;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getTitle() {
			return title;
		}
		public void setTitle(String title) {
			this.title = title;
		}
		public String getContent() {
			return content;
		}
		public void setContent(String content) {
			this.content = content;
		}
		public long getCreateTime() {
			return createTime;
		}
		public void setCreateTime(long createTime) {
			this.createTime = createTime;
		}
		public byte getChecked() {
			return checked;
		}
		public void setChecked(byte checked) {
			this.checked = checked;
		}
		public byte getReceived() {
			return received;
		}
		public void setReceived(byte received) {
			this.received = received;
		}
		public Object getExtra() {
			return extra;
		}
		public void setExtra(Object extra) {
			this.extra = extra;
		}
	}
	
	public static class MailAttach {
		protected int gold;
		protected int stone;
		protected int exp;
		protected List<ItemResponse> items;
		public MailAttach(com.xcity.game.mail.MailAttach ref) {
			this.gold = ref.getGold();
			this.stone = ref.getStone();
			this.exp = ref.getExp();
			if (ref.getItems() == null || ref.getItems().isEmpty()) {
				this.items = Collections.emptyList();
			} else {
				int size = ref.getItems().size();
				this.items = new ArrayList<ItemResponse>(size);
				for (int i = 0; i < size; i++) {
					this.items.add(new ItemResponse(ref.getItems().get(i)));
				}
			}
		}
		public int getGold() {
			return gold;
		}
		public void setGold(int gold) {
			this.gold = gold;
		}
		public int getStone() {
			return stone;
		}
		public void setStone(int stone) {
			this.stone = stone;
		}
		public int getExp() {
			return exp;
		}
		public void setExp(int exp) {
			this.exp = exp;
		}
		public List<ItemResponse> getItems() {
			return items;
		}
		public void setItems(List<ItemResponse> items) {
			this.items = items;
		}
	}
	
	public MailListResponse(int serial, List<com.xcity.db.entity.Mail> srcs) {
		super(serial);
		int size = srcs != null ? srcs.size() : 0;
		List<Mail> mails = new ArrayList<Mail>(size);
		for (int i = 0; i < size; i++) {
			com.xcity.db.entity.Mail src = srcs.get(i);
			Mail m = new Mail();
			m.roleId = src.getDestId();
			m.id = src.getId();
			m.title = src.getTitle();
			m.content = src.getContent();
			m.createTime = src.getPostTime().getTime();
			m.checked = (byte) (com.xcity.db.entity.Mail.STATE_UNREAD == src.getState() ? 0 : 1);
			com.xcity.game.mail.MailAttach attach = src.getAttachment();
			m.received = (byte) (com.xcity.db.entity.Mail.STATE_RECEIVED == src.getState()
								|| attach == null
								|| attach.isEmpty()
								? 1 : 0);
			m.extra = attach != null && !attach.isEmpty() ? new MailAttach(attach) : "";
			mails.add(m);
		}
		this.mails = mails;
	}

	public List<Mail> getMails() {
		return mails;
	}

	public void setMails(List<Mail> mails) {
		this.mails = mails;
	}
	
}
